//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.system;

import com.alibaba.fastjson.JSON;
import eu.bitwalker.useragentutils.UserAgent;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.TimerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaukie.base.core.entity.XLoginInfo;
import org.yaukie.base.core.entity.XOperLog;
import org.yaukie.base.core.service.api.XLoginInfoService;
import org.yaukie.base.core.service.api.XOperLogService;
import org.yaukie.base.util.IpUtil;
import org.yaukie.base.util.SpringContextUtil;

public class ASyncFactory {
    private static final Logger log = LoggerFactory.getLogger(ASyncFactory.class);

    public ASyncFactory() {
    }

    public static TimerTask recordLoginInfo(final String userName, final String status, final String msg, final Object... arg) {
        final String ipAddr = IpUtil.getIpAddr(SpringContextUtil.getRequest());
        final UserAgent userAgent = UserAgent.parseUserAgentString(SpringContextUtil.getRequest().getHeader("User-Agent"));
        return new TimerTask() {
            public void run() {
                try {
                    XLoginInfoService xLoginInfoService = (XLoginInfoService)SpringContextUtil.getBean(XLoginInfoService.class);
                    XLoginInfo xLoginInfo = new XLoginInfo();
                    xLoginInfo.setUserName(userName);
                    xLoginInfo.setIpaddr(ipAddr);
                    xLoginInfo.setLoginLocation(IpUtil.getRealAddressByIP(ipAddr));
                    xLoginInfo.setBrowser(userAgent.getBrowser().getName());
                    xLoginInfo.setOs(userAgent.getOperatingSystem().getName());
                    xLoginInfo.setStatus(status);
                    xLoginInfo.setMsg(msg);
                    ASyncFactory.log.info("监控用户登录日志打印:\r\n{}", JSON.toJSONString(xLoginInfo), JSON.toJSON(arg));
                    xLoginInfoService.insert(xLoginInfo);
                } catch (Exception var3) {
                    if (ASyncFactory.log.isErrorEnabled()) {
                        StringWriter sw = new StringWriter();
                        var3.printStackTrace(new PrintWriter(sw));
                        ASyncFactory.log.error("用户登录采集失败,原因{}", sw.toString().substring(0, 1800));
                    }
                }

            }
        };
    }

    public static TimerTask recordBehaviorInfo(final XOperLog xOperLog) {
        return new TimerTask() {
            public void run() {
                XOperLogService xOperLogService = (XOperLogService)SpringContextUtil.getBean(XOperLogService.class);
                xOperLogService.insert(xOperLog);
            }
        };
    }
}
